package com.ebt.m.repository.provider;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.widget.ImageView;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.data.bean.EbtFile;
import com.ebt.m.repository.provider.FileTypeHelper.FileTypeAll;
import com.ebt.m.utils.fileutils.FileUtils;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;

import java.io.File;

public class RepositoryProvider {
    private Context context;
    FileIconHelper iconHelper;

    public RepositoryProvider(Context context) {
        this.context = context;
        iconHelper = new FileIconHelper(context);
    }

    /**
     * 获取封面背景
     */
    public int getCoverBg(FileTypeAll type) {
        String result = null;
        switch (type) {
            case FILE_TYPE_IMAGE:// image
                result = "#ffffff";
                break;
            case FILE_TYPE_VIDEO:// video
                result = "#002d8a";
                break;
            case FILE_TYPE_AUDIO:// audio
                result = "#653ebf";
                break;
            case FILE_TYPE_PPT:// ppt
                result = "#e87731";
                break;
            case FILE_TYPE_WORD:// word
                result = "#2f93e8";
                break;
            case FILE_TYPE_EXCEL:// excel
                result = "#00a51b";
                break;
            case FILE_TYPE_TEXT:// text
                result = "#2f93e8";
                break;
            case FILE_TYPE_APP:// 应用程序
                result = "#019eaf";
                break;
            case FILE_TYPE_APK:// apk文件
                result = "#019eaf";
                break;
            case FILE_TYPE_SHORTCUT:// 桌面快捷
                result = "#f2f1f9";
                break;
            case FILE_TYPE_PDF:// pdf
                result = "#be1d49";
                break;
            case FILE_TYPE_WEB:// web
                result = "#e39f18";
                break;
            case FILE_TYPE_OTHER:// 未知文件类型
                result = "#404040";
                break;
            default:
                result = "#ffffff";
                break;
        }
        return Color.parseColor(result);
    }

    /**
     * 设置缩略图
     */
    public void setThumbnailImage(ImageView thumbnail, ImageView thumbnailInner, EbtFile file) {
        FileTypeAll fileType = file.type;
        thumbnail.setImageBitmap(null);
        thumbnailInner.setImageBitmap(null);
        thumbnailInner.setBackgroundDrawable(null);
        thumbnail.setBackgroundColor(new RepositoryProvider(context).getCoverBg(fileType));
        switch (fileType) {
            case FILE_TYPE_IMAGE:
                String url = Uri.decode(file.path);
                ImageLoaderUtil.getInstance().loadImage(AppContext.getInstance(), new ImageLoader.Builder().url(url).imgView(thumbnail).build());
                break;
            case FILE_TYPE_VIDEO:
                try {
                    File f = new File(file.path);
                    if (!f.exists()) {
                        thumbnail.setImageResource(R.drawable.repository_file_broken);
                    } else {
                        iconHelper.setIcon(file, Util.FLAG_VIEW_GRID, null, thumbnailInner, thumbnail);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    thumbnail.setImageResource(R.drawable.repository_file_broken);
                }
                break;
            case FILE_TYPE_APK:
                thumbnailInner.setImageDrawable(FileUtils.getApkIcon(context, file.path));
                break;
            case FILE_TYPE_APP:
                if (file.packageName != null && file.packageName != null) {
                    thumbnailInner.setImageDrawable(FileUtils.getAppIcon(context, file.packageName, file.activityName));
                }
                break;
            case FILE_TYPE_SHORTCUT:
                byte[] bb = file.shortcutIcon;
                if (bb != null && bb.length > 0) {
                    thumbnailInner.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeByteArray(bb, 0, bb.length)));
                }
                break;
            default:
                thumbnailInner.setBackgroundResource(FileUtils.getSquareThumb(fileType));
                break;
        }

    }

    /**
     * 设置缩略图
     */
    public void setThumbnailImage(ImageView thumbnailInner, EbtFile file) {
        FileTypeAll fileType = file.type;
        thumbnailInner.setImageBitmap(null);
        thumbnailInner.setBackgroundDrawable(null);
        switch (fileType) {
            case FILE_TYPE_IMAGE:
                String url = Uri.decode(file.path);
                ImageLoaderUtil.getInstance().loadImage(AppContext.getInstance(), new ImageLoader.Builder().url(url).imgView(thumbnailInner).build());
                break;
            case FILE_TYPE_VIDEO:
                File f = new File(file.path);
                if (!f.exists()) {
                    thumbnailInner.setImageResource(R.drawable.repository_file_broken);
                } else {
                    iconHelper.setIcon(file, Util.FLAG_VIEW_GRID, null, thumbnailInner, thumbnailInner);
                }
                break;
            case FILE_TYPE_APK:
                thumbnailInner.setImageDrawable(FileUtils.getApkIcon(context, file.path));
                break;
            case FILE_TYPE_APP:
                if (file.packageName != null && file.packageName != null) {
                    thumbnailInner.setImageDrawable(FileUtils.getAppIcon(context, file.packageName, file.activityName));
                }
                break;
            case FILE_TYPE_SHORTCUT:
                byte[] bb = file.shortcutIcon;
                if (bb != null && bb.length > 0) {
                    thumbnailInner.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeByteArray(bb, 0, bb.length)));
                }
                break;
            default:
                thumbnailInner.setBackgroundResource(FileUtils.getSquareThumb(fileType));
                break;
        }

    }

    /**
     * 设置缩略图
     *
     * @param repository
     */
    /*public void setThumbnailImage(ImageView thumbnail, ImageView thumbnailInner, VRepository repository) {
		thumbnail.setImageBitmap(null);
		thumbnail.setBackgroundDrawable(null);
		thumbnailInner.setImageBitmap(null);
		thumbnailInner.setBackgroundDrawable(null);
		FileTypeAll fileType = FileTypeAll.valueOf(repository.getContentType());
		switch (fileType) {
		case FILE_TYPE_IMAGE:
			ImageMgr.getInstance().displayImage(thumbnail, repository.getLocalPath(), Scheme.FILE, ImageMgr.PIC_DEFALUT);
			break;
		case FILE_TYPE_VIDEO:
			File f = new File(repository.getLocalPath());
			if (!f.exists()) {
				thumbnail.setImageResource(R.drawable.repository_file_broken);
			} else {
				EbtFile file = new EbtFile();
				file.isFolder = false;
				file.path = repository.getLocalPath();
				iconHelper.setIcon(file, Util.FLAG_VIEW_GRID, null, thumbnailInner, thumbnail);
			}
			break;
		case FILE_TYPE_APK:
			thumbnailInner.setImageDrawable(FileUtils.getApkIcon(context, repository.getLocalPath()));
			break;
		case FILE_TYPE_APP:
			if (repository.getPackageName() != null && repository.getClassName() != null) {
				thumbnailInner.setImageDrawable(FileUtils.getAppIcon(context, repository.getPackageName(), repository.getClassName()));
			}
			break;
		case FILE_TYPE_SHORTCUT:
			byte[] bb = repository.getShortcutIcon();
			if (bb != null && bb.length > 0) {
				thumbnailInner.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeByteArray(bb, 0, bb.length)));
			}
			break;
		default:
			thumbnailInner.setBackgroundResource(FileUtils.getSquareThumb(fileType));
			break;
		}
		thumbnail.setBackgroundColor(new RepositoryProvider(context).getCoverBg(fileType));
	}*/

    /**
     * @param v
     *            预览文件
     */
	/*public void play(VRepository v) {
		if (v == null) {
			return;
		}
		EbtFile f = null;
		if (v.getContentType() == FileTypeAll.FILE_TYPE_SHORTCUT.getValue()) {
			f = new EbtFile();
			Intent intent = IntentUtils.getShortcutIntent(v.getIntentString());
			f.intentString = v.getIntentString();
			f.intent = intent;
			f.type = FileTypeAll.valueOf(v.getContentType());
		} else {
			if (v.getPackageName() != null && v.getPackageName().contains("com.ebt.app")) {
				UIHelper.makeToast(context, "请不要打开自身");
				return;
			}
			f = new EbtFile(v.getLocalPath(), v.getContentType(), v.getName(), v.getPackageName(), v.getClassName());
		}
		EventLogUtils.saveUserLog("REPOSITORY_ADDFILESVIEW_PLAY", "预览文件", "");
		FileUtils.previewFile(context, f);
	}*/

    /**
     *            预览文件
     */
	/*public void play(EbtFile file) {
		if (file.type == FileTypeAll.FILE_TYPE_SHORTCUT) {
			Intent intent = IntentUtils.getShortcutIntent(file.intentString);
			file.intentString = file.intentString;
			file.intent = intent;
		}
		EventLogUtils.saveUserLog("REPOSITORY_ADDFILESVIEW_PLAY", "预览文件", "");
		FileUtils.previewFile(context, file);
	}*/

}
